package com.zqw.controller;

import com.github.pagehelper.PageHelper;
import com.zqw.info.GoodsInfo;
import com.zqw.info.GoodsTypeInfo;
import com.zqw.service.GoodsService;
import com.zqw.ss.Page;
import com.zqw.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    //转发到index界面
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        PageHelper.startPage(1,8);
        List<GoodsInfo> goodsInfos = goodsService.queryGoods(new GoodsInfo());
        request.setAttribute("goodsList",goodsInfos);
        PageHelper.startPage(1,3);
        List<GoodsTypeInfo> goodsTypeInfos = goodsService.queryGoodsType();
        request.setAttribute("types",goodsTypeInfos);
        return "index";
    }

    //转发到商品列表界面
    @RequestMapping("shop")
    public String queryGoods(HttpServletRequest request,GoodsInfo goodsInfo){

        Page page = goodsService.queryGoodsPage(goodsInfo);
        request.setAttribute("pageInfo",page);
        request.setAttribute("goodsName",goodsInfo.getGoodsName());
        request.setAttribute("goodsType",goodsInfo.getGoodsType());
        request.setAttribute("goodsSex",goodsInfo.getGoodsSex());
        return "shop";
    }

    //转发到商品详情
    @RequestMapping("goodsDetails")
    public String goodsDetails(String gid,HttpServletRequest request){
        if(!StringUtil.isEmpty(gid)){
            GoodsInfo goodsInfo = goodsService.queryGoodsById(gid);
            request.setAttribute("goods",goodsInfo);
        }
        return "product-details";
    }

}
